package com.example.demo.scheduledtask;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.example.demo.common.Constant;
import com.example.demo.config.ErpConfig;
import com.example.demo.mapper.PoMpsinfoMapper;
import com.example.demo.mapper.WmsPurchaseMapper;
import com.example.demo.utils.SoapXmlUtil;
import com.example.demo.vo.MiscellaneousPostListVo;
import com.example.demo.vo.MiscellaneousPostVo;
import com.example.demo.vo.XmlResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : Zhang.yt
 * @date : 2024/7/29
 */
@Component
@Slf4j
@DS("wms")
public class MiscellaneousPostIn {
    @Resource
    private WmsPurchaseMapper wmsPurchaseMapper;

    @Autowired
    private SoapXmlUtil soapXmlUtil;

    @Autowired
    PoMpsinfoMapper poMpsinfoMapper;

    @Resource
    private ErpConfig erpConfig;

    /**
     * 其他(杂项)入库过账
     */
    @Scheduled(fixedDelay = 120 * 1000)//开启定时同步
//    @Async(value = "taskExecutor")
    public void MiscellaneousPostIn() {
        //查询没有回调的数据
        MiscellaneousPostVo warehouseTransferPost = wmsPurchaseMapper.getMiscellaneousPostIn();
//        WarehouseTransferPostVo warehouseTransferPost = new WarehouseTransferPostVo();
        if (ObjectUtils.isEmpty(warehouseTransferPost) || ObjectUtil.isEmpty(warehouseTransferPost.getInbadocno())) {
            return;
        }
        warehouseTransferPost.setInbasite(Constant.PMDSSITE_AMA);
        //如果有不同批次的数据则需要回写批次数据
        List<MiscellaneousPostListVo> list = wmsPurchaseMapper.getMiscellaneousPostInList(warehouseTransferPost.getInbadocno());
        String resMessage;
        if(CollUtil.isEmpty(list)){
            resMessage = soapXmlUtil.getSoapXml("MiscellaneousPost", "inba_t", "", warehouseTransferPost, null);
        }else {
            resMessage = soapXmlUtil.getSoapXml("MiscellaneousPost", "inba_t", "inbb_t", warehouseTransferPost, list);
        }

        try {
            //转换为soap消息

            log.info("soap其他(杂项)入库过账请求报文->{}", resMessage);

//            log.info(resMessage);
            //状态占用
            wmsPurchaseMapper.updateMiscellaneousPostIn(warehouseTransferPost.getInbadocno(), "erp待返回", 3);
            //发送soap消息，并解析结果
            String result;
            try {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.getUrl());
            } catch (Exception e) {
                result = soapXmlUtil.sendSoapMessageNew(resMessage, erpConfig.URL2);
            }
            log.info("soap其他(杂项)出入库过账回调返回报文->{}", result);
            XmlResp xmlResp = soapXmlUtil.analysisXml(result);
            if (xmlResp.getCode().equals("0")) {
                //更新相应表状态为已下发
                wmsPurchaseMapper.updateMiscellaneousPostIn(warehouseTransferPost.getInbadocno(), xmlResp.getData().get("inbadocno"), 1);
            } else {
                //更新相应表状态为已下发
                wmsPurchaseMapper.updateMiscellaneousPostIn(warehouseTransferPost.getInbadocno(), xmlResp.getMessage(), 2);
            }
        } catch (Exception e) {
            e.printStackTrace();
            wmsPurchaseMapper.updateMiscellaneousPostIn(warehouseTransferPost.getInbadocno(), "非常见的数据处理问题，请联系开发人员处理", 2);
        }
    }
}
